
package com.sita.tingterest.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.tingterest.fragment.DetailFragment;
import com.sita.tingterest.fragment.EvalFragment;

public class VideoPlayActivity extends Activity implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener {

    public SurfaceView surface_video;
    public RelativeLayout mLayout;
    public android.app.FragmentTransaction mTransaction;
    public android.app.FragmentManager mManager;
    public TextView tv_detail;
    public TextView tv_eval;
    public DetailFragment mDetailFragment;
    public EvalFragment mEvalFragment;
    public ImageView img_back;
    public ImageView img_cursor;
    public Bitmap cursor;
    private int offSet;
    private Matrix matrix = new Matrix();
    private int bmWidth;
    private TranslateAnimation animation;
    private SurfaceHolder holder;
    private MediaPlayer mp;
    private ImageView img_state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        initView();
        initeCursor();
        initData();
    }

    public void initView() {
        tv_detail = (TextView) findViewById(R.id.tv_detail);
        tv_eval = (TextView) findViewById(R.id.tv_evalue);
        img_back = (ImageView) findViewById(R.id.img_back);
        tv_eval.setOnClickListener(this);
        tv_detail.setOnClickListener(this);
        img_back.setOnClickListener(this);
        mManager = getFragmentManager();
        mLayout = (RelativeLayout) findViewById(R.id.title_video);
        mLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        surface_video = (SurfaceView) findViewById(R.id.surface_video);
        img_cursor = (ImageView) findViewById(R.id.cursor);
        holder = surface_video.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        img_state = (ImageView) findViewById(R.id.btn_play);
    }

    public void initData() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        img_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp.isPlaying()) {
                    img_state.setImageResource(R.drawable.btn_music_play);
                    mp.pause();
                } else {
                    mp.start();
                    img_state.setImageResource(R.drawable.btn_pause);
                }
            }
        });

        if (mDetailFragment == null) {
            mDetailFragment = new DetailFragment();
        }
        getFragmentManager().beginTransaction().add(android.R.id.tabcontent, mDetailFragment).commit();
        tv_detail.setTextColor(getResources().getColor(R.color.app_primary_color));
        AnimationSet animationSet = new AnimationSet(true);
        animation = new TranslateAnimation(
                offSet, offSet + bmWidth, 0, 0);
        animationSet.addAnimation(animation);
        animationSet.setFillBefore(false);
        animationSet.setFillAfter(true);
        animationSet.setDuration(100);
        img_cursor.startAnimation(animationSet);

    }

    @Override
    public void onClick(View v) {
        mManager = getFragmentManager();
        mTransaction = mManager.beginTransaction();
        AnimationSet animationSet = new AnimationSet(true);
        hiden(mTransaction);
        clean();
        switch (v.getId()) {
            case R.id.tv_detail:
                tv_detail.setTextColor(getResources().getColor(R.color.app_primary_color));
                if (mDetailFragment == null) {
                    mDetailFragment = new DetailFragment();
                    mTransaction.add(android.R.id.tabcontent, mDetailFragment);
                } else {
                    mTransaction.show(mDetailFragment);
                }

                animation = new TranslateAnimation(
                        offSet, offSet + bmWidth, 0, 0);
                animationSet.addAnimation(animation);
                animationSet.setFillBefore(false);
                animationSet.setFillAfter(true);
                animationSet.setDuration(100);
                img_cursor.startAnimation(animationSet);
                break;

            case R.id.tv_evalue:
                tv_eval.setTextColor(getResources().getColor(R.color.app_primary_color));
                if (mEvalFragment == null) {
                    mEvalFragment = new EvalFragment();
                    mTransaction.add(android.R.id.tabcontent, mEvalFragment);
                } else {
                    mTransaction.show(mEvalFragment);
                }

                animation = new TranslateAnimation(offSet * 2 + 2 * bmWidth,
                        offSet * 3 + 4 * bmWidth, 0, 0);
                animationSet.addAnimation(animation);
                animationSet.setFillBefore(false);
                animationSet.setFillAfter(true);
                animationSet.setDuration(100);
                img_cursor.startAnimation(animationSet);
                break;
        }
        mTransaction.commit();

    }

    public void hiden(android.app.FragmentTransaction transaction) {
        if (mDetailFragment != null) {
            transaction.hide(mDetailFragment);
        }
        if (mEvalFragment != null) {
            transaction.hide(mEvalFragment);
        }
    }

    public void clean() {
        tv_detail.setTextColor(getResources().getColor(R.color.clr_button_light));
        tv_eval.setTextColor(getResources().getColor(R.color.clr_button_light));
    }

    private void initeCursor() {
        //// TODO: 2016/1/12
        //下划线随选择滑动
        cursor = BitmapFactory.decodeResource(getResources(), R.drawable.button_common_selector);
        cursor.createBitmap(24, 5, Bitmap.Config.ARGB_8888);
        bmWidth = 24;

        DisplayMetrics dm;
        dm = getResources().getDisplayMetrics();

        offSet = (dm.widthPixels - 3 * bmWidth) / 6;
        matrix.setTranslate(offSet, 0);
        img_cursor.setImageMatrix(matrix); // 需要imageView的scaleType为matrix
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //setMeidaPlayer();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

//    private void setMeidaPlayer() {
//        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.leesa);
//        mp = new MediaPlayer();
//        try {
//            mp.setDataSource(getApplicationContext(), uri);
//            mp.setDisplay(holder);
//            mp.setOnPreparedListener(this);
//            mp.setOnSeekCompleteListener(this);
//            mp.prepare();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//        } catch (SecurityException e) {
//            e.printStackTrace();
//        } catch (IllegalStateException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    @Override
    public void onSeekComplete(MediaPlayer mp) {

    }

    /**
     * Function to get Progress percentage
     *
     * @param currentDuration
     * @param totalDuration
     */
    public int getProgressPercentage(long currentDuration, long totalDuration) {
        Double percentage = (double) 0;

        long currentSeconds = (int) (currentDuration / 1000);
        long totalSeconds = (int) (totalDuration / 1000);

        // calculating percentage
        percentage = (((double) currentSeconds) / totalSeconds) * 100;

        // return percentage
        return percentage.intValue();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp.release();
    }
}
